package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.bean.Result;
import com.yc.entity.Exam;
import com.yc.entity.ExamDetail;
import com.yc.entity.Paper;
import com.yc.entity.Question;
import com.yc.home.cloudother.mapper.ExamDetailMapper;
import com.yc.home.cloudother.mapper.ExamMapper;
import com.yc.home.cloudother.mapper.PaperMapper;
import com.yc.home.cloudother.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-10-12
 */
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ExamMapper examMapper;

    @Autowired
    ExamDetailMapper examDetailMapper;

    @Autowired
    PaperMapper paperMapper;

    @RequestMapping("get")
    public Exam get(int pid, int uid){
        return examMapper.findById(pid, uid);
    }

    @Transactional()
    @RequestMapping("commit")
    public Result commit(@RequestBody Exam exam){
        //查询题目信息
        //将题目循环
        int sum = 0;        //统计总分
        //查询各题分数
        Paper paper = paperMapper.selectById(exam.getPid());
        for (ExamDetail detail : exam.getDetails()) {
            Question question = questionMapper.selectById(detail.getQid());
            //用户答案
            String[] answers = detail.getAnswer().split(";");
            //正确答案
            String[] items = question.getItems().split(";");
            detail.setTf(0);
            if("填空题".equals(question.getType())){
                // 使用HashSet来存储split1中的元素，方便快速查找
                Set<String> set = new HashSet<>();
                for (String item : items) {
                    set.add(item);
                }
                //统计答对的元素
                Set<String> set1 = new HashSet<>();
                //判断
                for (String item : answers) {
                    if (set.contains(item)) {
                        set1.add(item);
                    }
                }
                //返回答对的
                if(set.equals(set1)){
                    detail.setTf(1);
                    sum += set.size() * paper.getTk();
                }else{
                    detail.setTf(0);
                    sum += set1.size() * paper.getTk();
                }
            }else if (question.getType().endsWith("选题")){
                //处理单选多选
                //循环标准答案，进行判断
                boolean isTrue = true;
                for (int i = 0; i < items.length; i++) {
                    if (items[i].startsWith("$")) {
                        //去掉$
                        items[i] = items[i].substring(1);
                    }else {
                        items[i] = " ";
                    }
                    if(items[i].equals(answers[i]) == false){
                        isTrue = false;
                        break;
                    }
                }
                if(isTrue){
                    sum += "单选题".equals(question.getType())?paper.getDk():paper.getDxs();
                    detail.setTf(1);
                }
            }else{
                //判断题
                detail.setTf(question.getItems().equals(detail.getAnswer())?1:0);
                if(detail.getTf() == 1){
                    sum += paper.getPd();
                }
            }
        }

        //总分
        exam.setScore(sum);

        examMapper.insert(exam);
        for (ExamDetail detail : exam.getDetails()) {
            detail.setEid(exam.getId());
            examDetailMapper.insert(detail);
        }
        return new Result(1 , "总分", sum);
    }

    @RequestMapping("unone")
    Result unone(String cid, String uid, String id){
        LambdaQueryWrapper<Exam> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Exam::getCid, cid).eq(Exam::getUid, uid).eq(Exam::getPid, id);
        Long aLong = examMapper.selectCount(lqw);
        if(aLong == 0){
            return new Result(1, "参加考试", null);
        }else{
            return new Result(0, "请勿重复参加！", null);
        }
    }

    @RequestMapping("getdata")
    Result getdata(String cid, String pid){
        LambdaQueryWrapper<Exam> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Exam::getCid, cid).eq(Exam::getPid, pid).orderByDesc(Exam::getScore);
        List<Exam> exams = examMapper.selectList(lqw);
        return new Result(1, "成绩", exams);
    }

    @RequestMapping("getcid")
    public Exam getcid(int pid, int uid, int cid){
        return examMapper.findByCid(pid, uid, cid);
    }

}

